'use strict';
import HTTPUtil from './HTTPUtil';
import ServerURL from './ServerURL';
import SRJudge from './SRJudge';
import Toast from 'react-native-toast';


export default class HttpTool {
    static postWithPath(path, paramsDic, isLoading, isResPrompt) {
        return new Promise(function (resolve, reject) {
            HttpTool.requestWithPath(path, paramsDic, isLoading, isResPrompt, 'post').then((ret) => {
                resolve(ret);
            }).catch((err) => {
                reject(err);
            });;
        });
    }
    static putWithPath(path, paramsDic, isLoading, isResPrompt) {
        return new Promise(function (resolve, reject) {
            HttpTool.requestWithPath(path, paramsDic, isLoading, isResPrompt, 'put').then((ret) => {
                resolve(ret);
            }).catch((err) => {
                reject(err);
            });;
        });
    }
    static getWithPath(path, paramsDic, isLoading, isResPrompt) {
        return new Promise(function (resolve, reject) {
            HttpTool.requestWithPath(path, paramsDic, isLoading, isResPrompt, 'get').then((ret) => {
                resolve(ret);
            }).catch((err) => {
                reject(err);
            });;
        });
    }
    static requestWithPath(path, params, isLoading, isResPrompt, way) {
        return new Promise(function (resolve, reject) {
            if (isLoading == 1) {
                Toast.showLoading();
            } else if (isLoading != 0) {
                Toast.showLoading(isLoading);
            }

            if (way === 'post') {
                HTTPUtil.post(path, params).then((json) => {
                    SRJudge.successful(json, isResPrompt);
                    resolve(json);
                }).catch((err) => {
                    console.log('err','err'+err)
                    SRJudge.failure(err, isResPrompt);
                    reject(err);
                });
            }
            if (way === 'get') {
                HTTPUtil.get(path, params).then((json) => {
                    //结果操作
                    SRJudge.successful(json, isResPrompt);
                    //缓存
                    resolve(json);
                }).catch((err) => {
                    SRJudge.failure(err, isResPrompt);
                    reject(err);
                });
            }
        });
    }
}